package com.douqu.game.activity.manager;

import com.douqu.game.activity.GameServer;
import com.douqu.game.activity.gui.MainFrame;
import com.douqu.game.activity.util.CosUtils;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.util.LogUtils;

import java.util.List;

/**
 * Created by bean on 2017/7/27.
 */
public class LoadManager {

    public static boolean loading = false;


    public static void testLoad()
    {
        LogUtils.info("Test Load-------------------------------------------");

        DataFactory.getInstance(true).clearData();

        LoadFactory.loadGameObject("data");

        DataFactory.getInstance(true).checkConfig();

        for(String error : DataFactory.getInstance(true).getErrorList())
        {
            MainFrame.println(error);
        }
    }

    public static void load()
    {
        loading = true;

        DataFactory.getInstance().clearData();

        LoadFactory.loadGameObject("data");

        DataFactory.getInstance().init();

        LoadFactory.loadConfig();

        LoadFactory.loadPlatform();

        loading = false;
    }


    public static boolean loadFromCos(String path, String version)
    {
        if(GameServer.getInstance().getActivityCampManager().getActivityCampModel().getVersion().equals(version))
        {
            LogUtils.warn("更新的版本跟服务器当前版本相同 -> " + version);
            return false;
        }
        boolean result = CosUtils.downloadFile(path);
        if (result)
        {
            LoadFactory.setTestLoad(true);

            LoadManager.testLoad();

            List<String> errorList = DataFactory.getInstance(true).getErrorList();

            LoadFactory.setTestLoad(false);

            if (errorList.size() == 0)
            {
                LogUtils.info("GM开始更新配置文件,服务器暂停使用=========================================");

                LoadManager.load();

                //通知战斗服务器更新配置
                GameServer.getInstance().sendToBattleServerUpdateConfig();

                GameServer.getInstance().getActivityCampManager().getActivityCampModel().setVersion(version);
            }

            LogUtils.info("更新配置文件结果 -> " + errorList);
        }

        return result;
    }





}
